package com.fuxi.cloud.desreport.render.a.br;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.fuxi.cloud.desreport.model.RenderInfo;
import com.fuxi.cloud.desreport.model.ReportDbInfo;
import com.fuxi.cloud.desreport.render.handler.CellsRenderHandler;
import com.fuxi.cloud.desreport.render.utils.RegexMatches;
import com.fuxi.cloud.desreport.render.utils.RenderUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

/**
 * a
 *
 * @Author: 扫地僧
 * @CreateTime: 2021-05-03
 */

@Component("tableGroupRightRenderStrategy")
public class a implements CellsRenderHandler {
    private static final Logger a = LoggerFactory.getLogger(a.class);

    public a() {
    }

    @Override
    public boolean support(JSONObject cell, String text) {
        return text.indexOf("customGroup") != -1;
    }

    @Override
    public void beforeRender(RenderInfo renderInfo, JSONObject cell, String text) {
        text = text.replace("#{", "").replace("${", "").replace("}", "");
        cell.put("text", text);
    }

    @Override
    public Map render(RenderInfo renderInfo, JSONObject row, JSONObject cells, JSONObject cell, String cellKey, String cellText, Map groupData) {
        Map var8 = renderInfo.getReport().getDataList();
        String var9 = RegexMatches.a(cellText, 1);
        String var10 = RegexMatches.a(cellText, 2);
        String var11 = RegexMatches.b(var10, 2).replace(")", "");
        ReportDbInfo var12 = (ReportDbInfo)var8.get(var9);
        List var13 = var12.getList();
        if (ObjectUtil.isNotEmpty(groupData)) {
            RenderUtil.a(groupData, var11);
        }

        Map var14 = RenderUtil.c(var13, var11);
        AtomicReference var15 = new AtomicReference(Integer.parseInt(cellKey));
        if (ObjectUtil.isEmpty(groupData)) {
            this.a(var14, cells, cell, var15);
        } else {
            this.a(groupData, cells, cell, var15);
        }

        return ObjectUtil.isNotEmpty(groupData) ? groupData : var14;
    }

    public void a(Map var1, JSONObject var2, JSONObject var3, AtomicReference<Integer> var4) {
        var1.forEach((var4x, var5) -> {
            if (var5 instanceof ArrayList) {
                JSONObject var6 = new JSONObject();
                var6.putAll(var3);
                var6.put("text", var4x);
                List var7 = (List)var5;
                if (ObjectUtil.isNotEmpty(var7)) {
                    Integer[] var8 = new Integer[]{0, var7.size() - 1};
                    var6.put("merges", var8);
                }

                var2.put(var4.toString(), var6);
                var4.getAndSet((Integer)var4.get() + 1 + var7.size() - 1);
            } else {
                this.a((Map)var5, var2, var3, var4);
            }

        });
    }
}

